﻿using System.Collections.Generic;
using System.Linq;

namespace Edu.ZJ
{
    public static class SiteUtil
    {
        private static List<SiteModel> list = new List<SiteModel>();

        static SiteUtil()
        {
            Init();
        }

        private static void Init()
        {
            string code1 = "ZJ";
            UserModel user1 = AccountUtil.GetUserByCode(code1);
            list.Add(new SiteModel()
            {
                Code = code1,
                Name = "组卷网",
                HomeUrl = "https://www.zujuan.com",
                Domain = "www.zujuan.com",
                NeedCookie = true,
                User = user1,
                Script = new FileModel("scripts", "get_zj_html.js"),
                Html = new FileModel("scripts", "zj_html.txt"),
                Cookie = new CookieModel("_identity", "www.zujuan.com", "cookie", "zj_cookie.txt"),
                Download = new FileModel("Files", "{0}.pdf"),
                FrameLoadEnd = ZJ_End
            });

            string code2 = "XK_ZJ";
            UserModel user2 = AccountUtil.GetUserByCode(code2);
            list.Add(new SiteModel()
            {
                Code = code2,
                Name = "学科组卷网",
                HomeUrl = "https://zujuan.xkw.com",
                Domain = "zujuan.xkw.com",
                User = user2,
                Script = new FileModel("scripts", "get_xkzj_html.js"),
                Html = new FileModel("scripts", "xkzj_html.txt"),
                Cookie = new CookieModel(),
                Download = new FileModel("Files", "{0}.pdf"),
                FrameLoadEnd = XK_ZJ_End
            });
        }

        public static void Update(List<AccountModel> list)
        {
            foreach (var item in list)
            {
                var site = GetSiteByCode(item.Code);
                if (site != null)
                {
                    site.User.Account = item.Account;
                    site.User.Password = item.Password;
                }
            }
        }

        public static List<SiteModel> GetSiteList()
        {
            return list;
        }

        public static SiteModel GetDefaultSite()
        {
            return list[1];
        }

        public static SiteModel GetSiteByCode(string code)
        {
            return list.FirstOrDefault(m => m.Code == code);
        }

        private static void XK_ZJ_End(string url, SiteModel site, ExtChromiumBrowser webBrowser)
        {
            if (string.IsNullOrEmpty(site.User.Account) || string.IsNullOrEmpty(site.User.Password))
            {
                return;
            }

            if (url.Contains("/login"))
            {
                webBrowser.ExecuteJs("$('.another').trigger('click');");

                webBrowser.SetInputValue("username", site.User.Account);
                webBrowser.SetInputValue("password", site.User.Password);

                webBrowser.ExecuteJs("accountLogin();");

                webBrowser.ExecuteJs("$('#accountLoginForm .submit').click()");
            }
        }

        private static bool needRefreshCookie = false;
        private static void ZJ_End(string url, SiteModel site, ExtChromiumBrowser webBrowser)
        {
            if (string.IsNullOrEmpty(site.User.Account) || string.IsNullOrEmpty(site.User.Password))
            {
                return;
            }

            if (url.Contains("/login"))
            {
                webBrowser.SetInputValue("user-name", site.User.Account);
                webBrowser.SetInputValue("user-pwd", site.User.Password);

                webBrowser.BtnClickById("submit-btn");
                needRefreshCookie = true;
            }
            else
            {
                if (needRefreshCookie)
                {
                    var cookieManager = CefSharp.Cef.GetGlobalCookieManager();

                    CookieVisitor visitor = new CookieVisitor();
                    visitor.SendCookie += (cookie) =>
                    {
                        if (cookie.Name == site.Cookie.CookieName)
                        {
                            string content = $"{cookie.Value}|{cookie.Expires}";
                            FileUtil.WriteFileContent(content, site.Cookie.DirName, site.Cookie.FileName);
                            needRefreshCookie = false;
                        }
                    };
                    cookieManager.VisitAllCookies(visitor);
                }
            }
        }
    }
}
